package com.zluyuer.ermu.msg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HookManager {
    
    private static HookManager instance = new HookManager();
    
    public static HookManager getInstance() {
        return instance;
    }

    private Map<String, List<Hook>> preHooks = new HashMap<String, List<Hook>>();
    private Map<String, List<Hook>> postHooks = new HashMap<String, List<Hook>>();
    private Map<String, Hook> errorHooks = new HashMap<String, Hook>();
    
    public void addExecuteHook(String msgCode, Hook hook, boolean isPost) {
        Map<String, List<Hook>> hooks = isPost ? postHooks : preHooks;
        if (!hooks.containsKey(msgCode)) {
            hooks.put(msgCode, new ArrayList<Hook>());
        }
        hooks.get(msgCode).add(hook);
    }
    
    public void addPreHook(String msgCode, Hook hook) {
        addExecuteHook(msgCode, hook, false);
    }
    
    public void addPostHook(String msgCode, Hook hook) {
        addExecuteHook(msgCode, hook, true);
    }
    
    public List<Hook> getPreHooks(String msgCode) {
        return preHooks.containsKey(msgCode) ? preHooks.get(msgCode) : new ArrayList<Hook>();
    }
    
    public List<Hook> getPostHooks(String msgCode) {
        return postHooks.containsKey(msgCode) ? postHooks.get(msgCode) : new ArrayList<Hook>();
    }
    
    public void setErrorHook(String msgCode, Hook hook) {
        errorHooks.put(msgCode, hook);
    }
    
    public Hook getErrorHook(String msgCode) {
        return errorHooks.get(msgCode);
    }
}
